"""Defines data models that need database persistence"""

from django.contrib.gis.db import models


class Dataset(models.Model):
  """Stores metadata about a dataset."""
  name = models.CharField(max_length=200, blank=True, null=True)
  type = models.CharField(max_length=200, blank=True, null=True)
  
  # Keeps the original uploaded filename, as a simple indication of whether a
  # duplicated file is uploaded
  filename = models.CharField(max_length=200, blank=True, null=True)
  description = models.TextField(blank=True, null=True)
  src_file = models.FileField('Source file', upload_to='dataset/', blank=True, null=True)
  upload_time = models.DateTimeField('date posted', auto_now=True, null=True)
  time_offset = models.FloatField(blank=True, null=True)
  
  class Meta:
    verbose_name_plural = 'Dataset'
    
  def __unicode__(self):
    return "Dataset %s" % self.name


class TrackPoint(models.Model):
  """"""
  point = models.PointField(srid=4326)
  timestamp = models.FloatField()
  elevation = models.FloatField(blank=True, null=True)
  track_ref = models.ForeignKey("Track", blank=True, null=True)
  
  objects = models.GeoManager()
  
  class Meta:
    verbose_name_plural = 'Track Points'

  def __unicode__(self):
    return "[%s,%s]" % (str(self.point),
                        str(self.timestamp))
  

class Track(models.Model):
  name = models.CharField(max_length=200)
  description = models.TextField(blank=True, null=True)
  timestamp = models.DateTimeField(blank=True, null=True)
  
  class Meta:
    verbose_name_plural = 'GPS Tracks'

  def __unicode__(self):
    return "GPS track '%s'" % self.name


class GeoPhoto(models.Model):
  """
  TODO: pull common fields into Photo class and extend it
  """
  title = models.CharField(max_length=200)
  time_taken = models.FloatField()
  time_post = models.DateTimeField('date posted', auto_now=True, null=True)
  url = models.URLField()
  dataset_ref = models.ForeignKey("Dataset", blank=True, null=True)
  media = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True, null=True)
  #location = models.ReferenceField(TrackPoint)
  
  class Meta:
    verbose_name_plural = 'Photos with a location'

  def __unicode__(self):
    return self.title


class GeoRecord(models.Model):
  point_geom = models.MultiPointField(srid=4326, blank=True, null=True)
  line_geom = models.MultiLineStringField(srid=4326, blank=True, null=True)
  polygon_geom = models.MultiPolygonField(srid=4326, blank=True, null=True)
  name = models.CharField(max_length=200, blank=True, null=True)
  description = models.TextField(blank=True, null=True)
  
  dataset_ref = models.ForeignKey("Dataset", blank=True, null=True)
  
  objects = models.GeoManager()